package com.ymatou.iapi.optrade.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.BizOperateLogEnum;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.ChangeLogisticsTypeReq;
import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.ChangeLogisticsTypeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderBizLogPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;

/**
 * 更新物流方式 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ChangeLogisticsType {

	private static ChangeLogisticsTypeReq codnbean;
	private static ChangeLogisticsTypeCall codncall;

	private static OrderShardingWapper database = new OrderShardingWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新物流方式");
	}

	@Before
	public void caseUp() {

		codnbean = new ChangeLogisticsTypeReq();
		codncall = new ChangeLogisticsTypeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(codncall.getOpurl());
		database.activeDeadCommunicationsLink(database);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/***
	 * 海外物流且是官方物流 || 海外物流且时效15天且是签约买手 || 第三方保税
	 * isNeedUploadIdcard为true
	 * 
	 */
	private void verifyDb(int oldLogisticsType, Integer oldLogisticsAging,
			boolean oldOfficialLogistics, boolean isNeedUploadIdcard) {

		long orderId = codnbean.getOrderId();
		
		if (codnbean.getLogisticsType() != LogisticsTypeEnum.OVERSEAS_DELIVERY.getCode()) {
			
			codnbean.setLogisticsAging(5);
			codnbean.setOfficialLogistics(false);
		}

		// 订单表(order)
		OrderPo ymtOrders = database.selectOrderbyOrderId(orderId);

		Logger.comment(String.format("验证订单表(order)表 orderId: %s", orderId));

		Logger.verifyEquals(codnbean.getLogisticsType(),
				ymtOrders.getLogisticsType(), "验证logisticsType");
		
		Logger.verifyEquals(codnbean.getLogisticsAging(),
				ymtOrders.getLogisticsAging(), "验证LogisticsAging");
		
		Logger.verifyEquals(codnbean.getOfficialLogistics(),
				ymtOrders.getOfficialLogistics(), "验证OfficialLogistics");

		Logger.verifyEquals(isNeedUploadIdcard,
				ymtOrders.getNeedUploadIdcard(), "验证needUploadIdcard");

		// 订单主状态操作日志表(order_status_log)
		OrderBizLogPo orderBizLogPo = database
				.selectOrderBizLogByOrderId(orderId);

		Logger.comment("验证订单业务操作记录表(order_biz_log)");

		String memo = String.format(
				"（物流方式：%s，时效：%s,官方：%s）修改为（物流方式：%s，时效：%s,官方：%s）",
				oldLogisticsType, Utils.zeroIfNull(oldLogisticsAging), oldOfficialLogistics,
				codnbean.getLogisticsType(), Utils.zeroIfNull(codnbean.getLogisticsAging()),
				codnbean.getOfficialLogistics());

		Logger.verifyEquals(memo, orderBizLogPo.getContent(), "验证数据库Content");

		Logger.verifyEquals(orderId, orderBizLogPo.getOrderId(), "验证数据库OrderId");

		Logger.verifyEquals(String.valueOf(orderId), orderBizLogPo.getBizNo(),
				"验证数据库BizNo");
		Logger.verifyEquals(BizOperateLogEnum.CHANGE_DELIVERY_TYPE.toString(),
				orderBizLogPo.getBizType(), "验证数据库BizType");
		Logger.verifyEquals(codnbean.getSellerId(),
				orderBizLogPo.getOperator(), "验证数据库Operator");
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_001() {
		Logger.start(true, "更新物流方式成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService.payOrder();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY
					.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);
			codnbean.setLogisticsAging(5);
			codnbean.setOfficialLogistics(true);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(200, codncall.getCode(), "验证Code");

			// 数据库验证
			verifyDb(LogisticsTypeEnum.OVERSEAS_DELIVERY.getCode(), 10, false,
					true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_002() {
		Logger.start(true, "订单号不存在");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = 222;
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.BondedDelivery
					.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);
			codnbean.setLogisticsAging(1);
			codnbean.setOfficialLogistics(false);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(201, codncall.getCode(), "验证Code");
			Logger.verifyEquals(101, codncall.getBCode(), "验证bcode");

			Logger.verifyEquals(
					String.format("订单号%d不存在", codnbean.getOrderId()),
					codncall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_003() {
		Logger.start(true, "订单不属于该seller");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = 123;

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.BondedDelivery
					.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);
			codnbean.setLogisticsAging(1);
			codnbean.setOfficialLogistics(false);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(201, codncall.getCode(), "验证code");
			Logger.verifyEquals(103, codncall.getBCode(), "验证bcode");

			Logger.verifyEquals(String.format("用户%d不是订单%d的所有者",
					codnbean.getSellerId(), codnbean.getOrderId()), codncall
					.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_004() {
		Logger.start(true, "订单号为null");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			long sellerId = 123;

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.BondedDelivery
					.getCode());

			// codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);
			codnbean.setLogisticsAging(1);
			codnbean.setOfficialLogistics(false);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(400, codncall.getCode(), "验证code");
			Logger.verifyEquals(100, codncall.getBCode(), "验证bcode");

			Logger.verifyEquals("orderId:orderId未提供", codncall.getMsg(),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_005() {
		Logger.start(true, "订单为下单,修改物流方式失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.BondedDelivery
					.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);
			codnbean.setLogisticsAging(1);
			codnbean.setOfficialLogistics(false);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(201, codncall.getCode(), "验证Code");
			Logger.verifyEquals(102, codncall.getBCode(), "验证bcode");

			Logger.verifyEquals(
					String.format("当前订单%s状态不匹配", codnbean.getOrderId()),
					codncall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_006() {
		Logger.start(true, "更新物流方式修改前由 需要上传身份证-->不用上传身份证");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.BondedDelivery);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();

			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);
			codnbean.setLogisticsAging(1);
			codnbean.setOfficialLogistics(false);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(200, codncall.getCode(), "验证Code");

			// 数据库验证
			verifyDb(LogisticsTypeEnum.BondedDelivery.getCode(), null, false,
					false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_007() {
		Logger.start(true, "更新物流方式修改前由 不需要上传身份证-->需要上传身份证，更新成第三方保税");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();

			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.BondedDelivery.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);
			codnbean.setOfficialLogistics(false);
			codnbean.setLogisticsAging(null);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(200, codncall.getCode(), "验证Code");

			// 数据库验证
			verifyDb(LogisticsTypeEnum.OVERSEAS_DELIVERY.getCode(), null,
					false, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_008() {
		Logger.start(true, "更新物流方式修改前由 不需要上传身份证-->不需要上传身份证");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();

			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);
			codnbean.setLogisticsAging(5);
			codnbean.setOfficialLogistics(false);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(200, codncall.getCode(), "验证Code");

			// 数据库验证
			verifyDb(LogisticsTypeEnum.OVERSEAS_DELIVERY.getCode(), null,
					false, false);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_009() {
		Logger.start(true, "更新物流方式修改前由 需要上传身份证-->需要上传身份证，海外物流且OfficialLogistics设置为true");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.BondedDelivery);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();

			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);
			codnbean.setLogisticsAging(5);
			codnbean.setOfficialLogistics(true);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(200, codncall.getCode(), "验证Code");

			// 数据库验证
			verifyDb(LogisticsTypeEnum.BondedDelivery.getCode(), null,
					false, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_011() {
		Logger.start(true, "签约买手更新物流方式修改前由 不需要上传身份证-->需要上传身份证，更新成海外物流且时效15天");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService.placeOrder(
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3383, "wesper");
			
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();

			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);
			codnbean.setOfficialLogistics(false);
			codnbean.setLogisticsAging(15);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(200, codncall.getCode(), "验证Code");

			// 数据库验证
			verifyDb(LogisticsTypeEnum.OVERSEAS_DELIVERY.getCode(), 10,
					false, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}